//
//  SetupVC.m
//  jzyStudents
//
//  Created by 任辰东 on 2022/9/22.
//

#import "SetupVC.h"
#import "RCDTextFeidView.h"
#import "RCDCornerBtn.h"
#import "RCDCachesManager.h"
#import "FeedbackVC.h"
#import "RCDAlertView.h"
#import "UIView+ShadowPath.h"
#import "MineApis.h"
#import "AboutVC.h"
#import "UpdatePswVC.h"

@interface SetupVC ()
@property (nonatomic, strong) RCDTextFeidView *aboutView;
@property (nonatomic, strong) RCDTextFeidView *cacheView;
@property (nonatomic, strong) UIControl *cacheTouView;
@property (nonatomic, strong) RCDTextFeidView *updatePwsView;
@property (nonatomic, strong) RCDTextFeidView *feedbackView;
@property (nonatomic, strong) RCDCornerBtn *logoutBtn;
@end

@implementation SetupVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"设置";
    
    WeakSelf
    [self.logoutBtn setClickBlock:^(UIButton * _Nonnull btn) {
        StrongSelf
        [GVUserDefaults standardUserDefaults].isLoginSuccess = @(0);
        [JzyTools clearAllUserDefaultsData];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)pushSwichAction:(UISwitch *)switchBtn
{
    if (switchBtn.on) {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }else{
        [[UIApplication sharedApplication] unregisterForRemoteNotifications];
    }
}

- (void)cacheViewAction
{
    BOOL isClear = [[RCDCachesManager sharedManager] clearCaches];
    if (isClear) {
        [self showCompletionHUDWithMessage:@"成功清除" completion:NULL];
    }else{
        [self showWarningHUDWithMessage:@"清除失败" completion:NULL];
    }
    self.cacheView.model.content =  [[RCDCachesManager sharedManager] getAllTheCacheFileSize];
}

- (void)feebbackViewAction
{
    if ([GVUserDefaults standardUserDefaults].isLoginSuccess.integerValue == 0) {
        LoginVC *loginVC = [LoginVC new];
        [self.navigationController pushViewController:loginVC animated:YES];
    }else{
        FeedbackVC *feedbackVC = [FeedbackVC new];
        [self.navigationController pushViewController:feedbackVC animated:YES];
    }
}

-(void)updatePwsViewAction
{
    DLog(@"修改密码");
    UpdatePswVC *updateVC = [UpdatePswVC new];
    [self.navigationController pushViewController:updateVC animated:YES];
}

-(void)aboutViewAction
{
    AboutVC *aboutVC = [AboutVC new];
    [self.navigationController pushViewController:aboutVC animated:YES];
}

-(void)setupViews
{
//    [self.bgScrollView.contentView addSubview:self.aboutView];
    [self.bgScrollView.contentView addSubview:self.cacheView];
    [self.bgScrollView.contentView addSubview:self.cacheTouView];
    [self.bgScrollView.contentView addSubview:self.updatePwsView];
    [self.bgScrollView.contentView addSubview:self.feedbackView];
    [self.bgScrollView.contentView addSubview:self.logoutBtn];

    [self.cacheTouView addTarget:self action:@selector(cacheViewAction) forControlEvents:UIControlEventTouchUpInside];
    [self.updatePwsView setTouchDownTarget:self action:@selector(updatePwsViewAction)];
    [self.feedbackView setTouchDownTarget:self action:@selector(feebbackViewAction)];
    [self.aboutView setTouchDownTarget:self action:@selector(aboutViewAction)];
    
    if ([GVUserDefaults standardUserDefaults].isLoginSuccess.integerValue == 0) {
        self.logoutBtn.hidden = YES;
    }
}

-(void)setupLayout
{
    
    [self.cacheView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.bgScrollView.contentView).offset(0);
        make.left.equalTo(self.bgScrollView.contentView).offset(-6);
        make.right.equalTo(self.bgScrollView.contentView);
        make.height.mas_equalTo(50);
    }];
    
    [self.cacheTouView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.bgScrollView.contentView).offset(0);
        make.left.equalTo(self.bgScrollView.contentView).offset(-6);
        make.right.equalTo(self.bgScrollView.contentView);
        make.height.mas_equalTo(50);
    }];
    
    [self.updatePwsView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.cacheView.mas_bottom).offset(0);
        make.left.equalTo(self.bgScrollView.contentView).offset(-6);
        make.right.equalTo(self.bgScrollView.contentView);
        make.height.mas_equalTo(50);
    }];

    [self.feedbackView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.updatePwsView.mas_bottom).offset(0);
        make.left.equalTo(self.bgScrollView.contentView).offset(-6);
        make.right.equalTo(self.bgScrollView.contentView);
        make.height.mas_equalTo(50);
    }];
    
//    [self.aboutView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.top.equalTo(self.feedbackView.mas_bottom).offset(0);
//        make.left.equalTo(self.bgScrollView.contentView).offset(-6);
//        make.right.equalTo(self.bgScrollView.contentView);
//        make.height.mas_equalTo(50);
//    }];


    [self.logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.feedbackView.mas_bottom).offset(40);
        make.left.equalTo(self.bgScrollView.contentView).offset(15);
        make.right.equalTo(self.bgScrollView.contentView).offset(-15);
        make.height.mas_equalTo(40);
        make.bottom.equalTo(self.bgScrollView.contentView).offset(-30 + kBottomOffset);
    }];
    
}

#pragma mark -- lazyload

-(RCDTextFeidView *)aboutView
{
    if (!_aboutView) {
        RCDTextFeidViewModel *model = [RCDTextFeidViewModel new];
        model.leftTitle = @"关于";
        model.leftTitleFont = UIFontSize(15);
        model.leftTitleColor = UIColorFromHex(kColorTitle);
        model.rightImgPath = @"arrow";
        _aboutView = [[RCDTextFeidView alloc]initWithModel:model];
        [_aboutView addBottomLine];
        _aboutView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _aboutView;
}

-(RCDTextFeidView *)cacheView
{
    if (!_cacheView) {
        RCDTextFeidViewModel *model = [RCDTextFeidViewModel new];
        model.leftTitle = @"清除本地缓存";
        model.leftTitleFont = UIFontSize(15);
        model.leftTitleColor = UIColorFromHex(kColorTitle);
        model.isNotEnable = YES;
        model.contentFont = UIFontSize(15);
        model.contentColor = UIColorFromHex(kColorTitle);
        model.alignment = NSTextAlignmentRight;
        model.content = [[RCDCachesManager sharedManager] getAllTheCacheFileSize];
        _cacheView = [[RCDTextFeidView alloc]initWithModel:model];
        [_cacheView addTopLine];
        [_cacheView addBottomLineWithEdge:UIEdgeInsetsMake(0, 15, 0, 15)];
        _cacheView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _cacheView;
}

-(UIControl *)cacheTouView
{
    if (!_cacheTouView) {
        _cacheTouView = [UIControl new];
        _cacheTouView.backgroundColor = [UIColor clearColor];
    }
    return _cacheTouView;
}

-(RCDTextFeidView *)updatePwsView
{
    if (!_updatePwsView) {
        RCDTextFeidViewModel *model = [RCDTextFeidViewModel new];
        model.leftTitle = @"修改密码";
        model.leftTitleFont = UIFontSize(15);
        model.leftTitleColor = UIColorFromHex(kColorTitle);
        model.rightImgPath = @"arrow";
        _updatePwsView = [[RCDTextFeidView alloc]initWithModel:model];
        [_updatePwsView addBottomLineWithEdge:UIEdgeInsetsMake(0, 15, 0, 15)];
        _updatePwsView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _updatePwsView;
}

-(RCDTextFeidView *)feedbackView
{
    if (!_feedbackView) {
        RCDTextFeidViewModel *model = [RCDTextFeidViewModel new];
        model.leftTitle = @"用户反馈";
        model.leftTitleFont = UIFontSize(15);
        model.leftTitleColor = UIColorFromHex(kColorTitle);
        model.rightImgPath = @"arrow";
        _feedbackView = [[RCDTextFeidView alloc]initWithModel:model];
        [_feedbackView addBottomLineWithEdge:UIEdgeInsetsMake(0, 15, 0, 15)];
        _feedbackView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _feedbackView;
}

-(RCDCornerBtn *)logoutBtn
{
    if (!_logoutBtn) {
        _logoutBtn = [RCDCornerBtn new];
        [_logoutBtn setTitleString:@"退出登录"];
        [_logoutBtn setTitleFont:16];
        _logoutBtn.cornerRadius = 20;
    }
    return _logoutBtn;
}


@end
